package gestao_proj;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * A classe GestaoTarefas e usada para adicionar tarefas ao Projecto
 * 
 * @author Omar Castro
 * 
 */
@SuppressWarnings("serial")
public class GestaoTarefas extends JPanel implements ActionListener {
	private Projecto proj;
	private JList precedence_sel, sons_sel;
	private DefaultListModel l_prec, l_sons;
	private JTextField text;
	private JSpinner duracao;
	private Arc2D arc;
	private RadialGradientPaint p;
	private Painel top;

	public GestaoTarefas(Projecto proj, Painel panel) {
		this.proj = proj;
		this.setLayout(null);
		top = panel;
		l_prec = new DefaultListModel();
		l_sons = new DefaultListModel();
		actualize();
		text = new JTextField(20);
		text.setOpaque(false);
		text.addActionListener(this);
		SpinnerNumberModel limiter = new SpinnerNumberModel(1, 1, 999, 1);
		duracao = new JSpinner(limiter);
		arc = new Arc2D.Double();
		precedence_sel = new JList(l_prec);
		sons_sel = new JList(l_sons);
		text.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLoweredBevelBorder(), "Nome da Tarefa",
				TitledBorder.CENTER, TitledBorder.ABOVE_TOP));
		precedence_sel.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(), "Precedencias", TitledBorder.CENTER,
				TitledBorder.ABOVE_TOP));
		sons_sel.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(), "Ascendencias", TitledBorder.CENTER,
				TitledBorder.ABOVE_TOP));

		precedence_sel.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				listrefresh(l_sons, precedence_sel.getSelectedValues());
			}
		});
		this.add(precedence_sel, BorderLayout.WEST);
		this.add(sons_sel, BorderLayout.EAST);
		this.add(text);
		this.add(duracao);
	}

	public void paintComponent(Graphics g) {
		Painel.paintback(g, this.getVisibleRect());
		int midx = this.getWidth() >> 1, midy = this.getHeight() >> 1;
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		arc.setArc(midx - 100, midy - 100, 200, 200, 0, 360, Arc2D.OPEN);
		p = new RadialGradientPaint(new Point2D.Float(midx + 20, midy - 50),
				300, new Point2D.Float(midx + 30, midy - 60), new float[] {
						0.0f, 0.5f }, new Color[] { Color.WHITE, Color.gray },
				CycleMethod.NO_CYCLE);
		precedence_sel.setBounds((int) arc.getMinX() - 300,
				(int) arc.getMinY(), 200, (int) arc.getHeight());
		sons_sel.setBounds((int) arc.getMaxX() + 100, (int) arc.getMinY(), 200,
				(int) arc.getHeight());
		text.setBounds(midx - 75, midy - 50, 150, 40);
		duracao.setBounds(midx - 25, midy + 20, 50, 20);
		g2.setPaint(p);
		g2.fill(arc);
		g2.setPaint(Color.black);
		g2.drawString("duracao", midx - 20, midy + 15);
	}

	public void actionPerformed(ActionEvent e) {
		if (!text.getText().isEmpty()) {
			Tarefa tar = new Tarefa(text.getText(), (Integer) duracao
					.getValue());
			for (int k : precedence_sel.getSelectedIndices())
				tar.add_dependences(proj.tarefas.get(k));
			for (Object k : sons_sel.getSelectedValues())
				for (Tarefa i : proj.tarefas)
					if (i.getnome().equals((String) k))
						i.add_dependences(tar);
			proj.add(tar);
			top.actualize();
		}
	}

	/**
	 * Actualiza as duas listas;
	 */
	public void actualize() {
		listrefresh(l_sons, null);
		listrefresh(l_prec, null);
	}

	/**
	 * Actualiza a lista removendo os objectos seleccionados pela outra lista,
	 * 
	 * 
	 * @param list
	 *            - a lista a ser actualixada
	 * @param sel
	 *            - objetos que vaoser retiados da lista;
	 * 
	 * @caso sel nao tenha objectos ou seja nulo a lista e toda actualizada
	 */
	public void listrefresh(DefaultListModel list, Object[] sel) {
		int a = (list == l_prec) ? 1 : 0;
		boolean add = true;
		list.clear();
		for (Tarefa k : proj.tarefas) {
			if (sel != null)
				for (Object i : sel)
					if (((String) i).equals(k.getnome())
							&& !((String) i).equals("INICIO")) {
						add = false;
					}
			if (add)
				list.addElement(k.getnome());
			add = true;
		}
		list.remove(a * (list.size() - 1));
	}

}
